package com.app.micaihu.view.newsdetail.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

import com.app.micaihu.custom.view.PhotoImageView;

import java.util.ArrayList;


/**
 * Created by Guo on 2016/8/3.
 * <p>
 * 新闻图片详情的viewpager的adapter
 */
public class NewsImageDetailAdapter extends PagerAdapter {


    private ArrayList<String> imageList;
    private PhotoImageView.ImagePhotoViewListener imagePhotoViewListener;

    public NewsImageDetailAdapter(ArrayList<String> imageList) {
        this.imageList = imageList;
    }


    @Override
    public int getCount() {

        return (imageList == null || imageList.size() <= 0) ? 0 : imageList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        PhotoImageView photoImageView = new PhotoImageView(container.getContext());


        photoImageView.loadImage(imageList.get(position), container.getContext());
        if (imagePhotoViewListener != null) {
            photoImageView.setImagePhotoViewListener(imagePhotoViewListener);
        }
        try {
            container.addView(photoImageView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return photoImageView;


    }


    public void setImageClickListener(PhotoImageView.ImagePhotoViewListener imagePhotoViewListener) {

        this.imagePhotoViewListener = imagePhotoViewListener;
    }


}
